#!/usr/bin/env python
#-*- coding: utf-8 -*-

import sys,os
import urllib,urllib2,re

def convert(lat1,lon1):
	print 'converting:',lat1,lon1
	url = "http://112.124.67.178:7081/MapFactoryServices/GPSService?x=%s&y=%s"%(lon1.strip(),lat1.strip())
	htmlH = urllib2.urlopen(url)
	htmlR = htmlH.read()
	print 'Got: ',htmlR.strip()
	fout.write(htmlR)

def processpoint(strlist):
	print strlist[0],':',strlist[1],',',strlist[2],'@',strlist[3]
	lat = strlist[1]
	lon = strlist[2]
	convert(lat,lon)
	
if __name__ == '__main__':
	print "Coverting GPS log..."

	gpsfilenamein = "./gps_M8N_20150112.txt"
	gpsfilenameout = "./gps_m8n_20150112out.txt"
	
	fin = open(gpsfilenamein, 'r+')
	fout = open(gpsfilenameout, 'w+')
	for  strline in  fin: 
		strlist = strline.split(',')
		processpoint(strlist)
	fin.close()
	fout.close()
	
	#print(htmlR)

	#f = open(gpsfilenamec, 'r+')
	#print hostNew
	#f.seek(0)
	#f.write(hostNew)
	#f.close()
	print 'ok'
